<template>
  <div class="admin-management">
    <!-- 头部 -->
    <div class="header">
      <div class="header-left">
        <h1>📚 要背词管理系统</h1>
      </div>
      <div class="header-right">
        <el-dropdown @command="handleCommand">
          <span class="user-info">
            <el-avatar 
              :size="32" 
              :src="userInfo.avatarUrl" 
              class="user-avatar"
            >
              {{ getUserInitial() }}
            </el-avatar>
            <span class="username">{{ userInfo.username || '管理员' }}</span>
            <el-icon class="el-icon--right"><arrow-down /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="profile">个人信息</el-dropdown-item>
              <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 侧边导航 -->
      <div class="sidebar">
        <el-menu
          :default-active="activeMenu"
          class="menu"
          @select="handleMenuSelect"
        >
          <el-menu-item index="dashboard">
            <el-icon><DataLine /></el-icon>
            <span>数据概览</span>
          </el-menu-item>
          <el-menu-item index="users">
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </el-menu-item>
          <el-menu-item index="words">
            <el-icon><Reading /></el-icon>
            <span>单词管理</span>
          </el-menu-item>
          <el-menu-item index="wordbooks">
            <el-icon><Collection /></el-icon>
            <span>词书管理</span>
          </el-menu-item>
          <el-menu-item index="study-records">
            <el-icon><Histogram /></el-icon>
            <span>学习记录</span>
          </el-menu-item>
          <el-menu-item index="system-logs">
            <el-icon><Document /></el-icon>
            <span>系统日志</span>
          </el-menu-item>
          <el-menu-item index="profile">
            <el-icon><User /></el-icon>
            <span>个人信息</span>
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 内容区域 -->
      <div class="content">
        <!-- 数据概览 -->
        <div v-if="activeMenu === 'dashboard'" class="content-panel">
          <DashboardView />
        </div>

        <!-- 用户管理 -->
        <div v-if="activeMenu === 'users'" class="content-panel">
          <UserManagement />
        </div>

        <!-- 单词管理 -->
        <div v-if="activeMenu === 'words'" class="content-panel">
          <WordManagement />
        </div>

        <!-- 词书管理 -->
        <div v-if="activeMenu === 'wordbooks'" class="content-panel">
          <WordbookManagement />
        </div>

        <!-- 学习记录 -->
        <div v-if="activeMenu === 'study-records'" class="content-panel">
          <StudyRecords />
        </div>

        <!-- 系统日志 -->
        <div v-if="activeMenu === 'system-logs'" class="content-panel">
          <SystemLogs />
        </div>

        <!-- 个人信息 -->
        <div v-if="activeMenu === 'profile'" class="content-panel">
          <AdminProfile />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  User,
  ArrowDown,
  DataLine,
  Reading,
  Collection,
  Histogram,
  Document
} from '@element-plus/icons-vue'

// 导入组件
import DashboardView from '../components/DashboardView.vue'
import UserManagement from '../components/UserManagement.vue'
import WordManagement from '../components/WordManagement.vue'
import WordbookManagement from '../components/WordbookManagement.vue'
import StudyRecords from '../components/StudyRecords.vue'
import SystemLogs from '../components/SystemLogs.vue'
import AdminProfile from '../components/AdminProfile.vue'

const router = useRouter()

// 用户信息
const userInfo = ref(JSON.parse(localStorage.getItem('userInfo') || '{}'))

// 当前激活的菜单
const activeMenu = ref('dashboard')

// 页面初始化
onMounted(() => {
  // 路由守卫已经处理了登录验证，这里不需要重复检查
  // 可以在这里做一些其他初始化工作
})

// 菜单选择
const handleMenuSelect = (index) => {
  activeMenu.value = index
}

// 获取用户名首字母
const getUserInitial = () => {
  const username = userInfo.value.username || userInfo.value.nickname || '管理员'
  return username.charAt(0).toUpperCase()
}

// 用户菜单操作
const handleCommand = (command) => {
  if (command === 'logout') {
    ElMessageBox.confirm('确认退出登录吗？', '退出确认', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
      ElMessage.success('已退出登录')
      router.push('/login')
    })
  } else if (command === 'profile') {
    // 切换到个人信息菜单
    activeMenu.value = 'profile'
  }
}
</script>

<style scoped>
.admin-management {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.header {
  background: white;
  padding: 0 24px;
  height: 64px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

.header-left h1 {
  margin: 0;
  color: #303133;
  font-size: 20px;
  font-weight: 600;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #606266;
  font-size: 14px;
  padding: 8px 12px;
  border-radius: 20px;
  transition: background-color 0.3s;
  gap: 8px;
}

.user-info:hover {
  background-color: #f5f5f5;
}

.user-avatar {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  font-weight: bold;
  flex-shrink: 0;
}

.username {
  font-weight: 500;
}

.user-info .el-icon {
  margin-left: 4px;
}

.main-content {
  display: flex;
  margin-top: 64px;
  min-height: calc(100vh - 64px);
}

.sidebar {
  width: 220px;
  background: white;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  flex-shrink: 0;
  position: fixed;
  top: 64px;
  left: 0;
  bottom: 0;
  overflow-y: auto;
}

.menu {
  border: none;
  height: 100%;
}

.content {
  flex: 1;
  margin-left: 220px;
  padding: 24px;
  overflow-y: auto;
}

.content-panel {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  min-height: calc(100vh - 112px);
}

:deep(.el-menu-item) {
  height: 56px;
  line-height: 56px;
  padding: 0 20px;
  display: flex;
  align-items: center;
}

:deep(.el-menu-item .el-icon) {
  margin-right: 8px;
  font-size: 18px;
}

:deep(.el-menu-item span) {
  font-size: 14px;
  white-space: nowrap;
}

:deep(.el-menu-item:hover) {
  background-color: #f5f5f5;
}

:deep(.el-menu-item.is-active) {
  background-color: #409eff;
  color: white;
}

:deep(.el-menu-item.is-active .el-icon) {
  color: white;
}
</style>

